/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.xiaoniu.forum.web.controller.app;

import com.xiaoniu.forum.form.BaseForm;
import com.xiaoniu.forum.form.UsernamePasswordResgisterForm;
import com.xiaoniu.forum.renren.common.exception.RRException;
import com.xiaoniu.forum.renren.common.utils.R;
import com.xiaoniu.forum.renren.common.validator.ValidatorUtils;
import com.xiaoniu.forum.form.PhoneRegisterForm;
import com.xiaoniu.forum.renren.modules.sys.service.SysCaptchaService;
import com.xiaoniu.forum.service.AppRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 注册
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api("APP注册接口")
public class AppRegisterController {
    @Autowired
    private AppRegisterService registerService;

    @Autowired
    private SysCaptchaService captchaService;

    @PostMapping("phoneRegister")
    @ApiOperation("注册")
    public R register(@RequestBody PhoneRegisterForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);
        captchaValidate(form);
        registerService.phoneRegister(form);
        return R.ok();
    }
    @PostMapping("usernameRegister")
    @ApiOperation("注册")
    public R register(@RequestBody UsernamePasswordResgisterForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);
        captchaValidate(form);
        registerService.usernamePasswordRegister(form);
        return R.ok();
    }

    //校验验证码
    private void captchaValidate(BaseForm form){
        boolean flag = captchaService.validate(form.getUuid(), form.getCaptcha());
        if (!flag){
            throw  new RRException("验证码错误");
        }
    }
}
